//
//  UIContentModeCell.swift
//  iPhone
//
//  Created by Toj on 6/12/23.
//

import UIKit

class UIContentModeCell: UICollectionViewCell {
    
    var cellModel: UIContentModeCellModel? { didSet {
        guard let model = cellModel else { return }
        let url = URL(string: model.url)
        
        imageView.contentMode = model.contentMode
        imageView.kf.setImage(with: url, placeholder: UIImage(named: model.placeholder))
        titleLabel.text = model.contentMode.title
        desLabel.text = model.contentMode.descrip
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.clipsToBounds = true
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.font = 15.to_systemFont
        view.textColor = .random
        return view
    }()
    
    private lazy var desLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.font = 13.to_systemFont
        view.textColor = .random
        return view
    }()
}

extension UIContentModeCell {
    
    private func setupAddSubviews() {
        contentView.addSubview(imageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(desLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        imageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView).inset(10)
        }
        
        desLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(contentView)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.bottom.equalTo(desLabel.snp.top)
            make.left.right.equalTo(contentView)
        }
    }
}

